package com.dianning.web.interceptor;

import java.util.*;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.util.RequestUtils;
import org.apache.struts2.ServletActionContext;
import org.springframework.context.support.ResourceBundleMessageSource;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.dianning.bean.UserInfo;
import com.dianning.comm.Constant;
import com.dianning.comm.DianningUtil;
import com.dianning.service.UserInfoService;
import com.dianning.web.servlet.UserCookie;
import com.dianning.web.servlet.UserSession;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
import org.apache.struts2.views.util.*;

public class UserPermissionInterceptor extends AbstractInterceptor {
	/**
	 * Logger for this class
	 */
	private static final Log logger = LogFactory.getLog(UserPermissionInterceptor.class);

	/**
	 *
	 */
	private static final long serialVersionUID = 1857640901215440713L;

	@Override
	public String intercept(ActionInvocation invocation) throws Exception {

		ActionContext ac = invocation.getInvocationContext();
		String actionName = "/" + ac.getName();

		String saction = "";

		Map map = ac.getParameters();
		String[] _saction = (String[]) map.get("action");
		if (_saction != null) {
			saction = _saction[0];
		}
		
		HttpServletRequest request = (HttpServletRequest) ac.get(ServletActionContext.HTTP_REQUEST);
		HttpServletResponse response = (HttpServletResponse) ac.get(ServletActionContext.HTTP_RESPONSE);
		ServletContext servletContext = (ServletContext) ac.get(ServletActionContext.SERVLET_CONTEXT);
		WebApplicationContext wc = WebApplicationContextUtils.getWebApplicationContext(servletContext);
		
		StringBuilder sb = new StringBuilder(request.getServletPath());
		UrlHelper helper = new DefaultUrlHelper();
		helper.buildParametersString(map, sb, "&");
		UserSession us = (UserSession) ac.getSession().get(Constant.USER_SESSION_KEY);
		//logger.info("actionName:" + actionName + ",saction:" + saction + ",uid:" + us.getUid() + ",url:" + sb.toString());
		//logger.info("actionName:" + actionName + ",saction:" + saction + ",uid:" + us.getUid());
		boolean havePermission = true;
		
		if(us.getUid()==0){
			UserCookie uc = new UserCookie(request, response);
			String auth = uc.getCookie(Constant.COOKIE_AUTOLOGIN_AUTH);
			if(StringUtils.isNotBlank(auth)){
				long uid = DianningUtil.decodeAuth(auth);
				UserInfoService userInfoService = (UserInfoService) wc.getBean("userInfoService");
				UserInfo ui = userInfoService.findUserInfoByUid(uid);
				us.setUid(ui.getId());
				us.setUsername(ui.getUsername());
				us.setName(ui.getName());
				ac.getSession().put(Constant.USER_SESSION_KEY, us);
				havePermission = true;
			}else{
				havePermission = false;
			}
		}else{
			havePermission = true;
		}
		if (havePermission) {
			return invocation.invoke();
		} else {
			ac.getValueStack().set("tourl", sb.toString());
			ac.getValueStack().set("action", "login");
			return "login";
		}
	}
}
